﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cricket.LinqData;
using Cricket.LinqModel;

public partial class MyProfile : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    Client client;    
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            HttpCookie cookie = Request.Cookies["Client"];
            if (cookie == null)
            {
                Response.Redirect("Login.aspx");
            }
            else
            {
                fillClient(cookie["ClientId"]);
            }
        }
    }

    private void fillClient(string ClientId)
    {
        client = new Client(_dataObject);
        client.Id = Convert.ToInt32(ClientId);
        client.GetClientById();

        if (client.Id != 0)
        {
            txtFirstName.Text = client.FirstName;
            txtLastName.Text = client.LastName;
            txtPhone.Text = client.Phone;
            txtEmailAddress.Text = client.Email;
            txtAddress.Text = client.Address;
            txtCity.Text = client.City;
            txtstate.Text = client.State;
            txtPinCode.Text = client.Zip;
            txtUsername.Text = client.Username;
            txtPassword.Attributes.Add("value", client.Password);            
            txtCommission.Text = client.CommissionRate;
            txtNECommission.Text = client.NECommission;
            txtLimit.Text = client.ActualLimit.ToString();
            txtMinLimit.Text = client.MinLimit.ToString();
            txtMaxLimit.Text = client.MaxLimit.ToString(); 
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        HttpCookie cookie = Request.Cookies["Client"];
        client = new Client(_dataObject);
        client.AdminId = Convert.ToInt32(cookie["AdminId"]);
        client.FirstName = txtFirstName.Text;
        client.LastName = txtLastName.Text;
        client.Phone = txtPhone.Text;
        client.Email = txtEmailAddress.Text;
        client.Address = txtAddress.Text;
        client.City = txtCity.Text;
        client.State = txtstate.Text;
        client.Zip = txtPinCode.Text;
        client.Username = txtUsername.Text;
        client.Password = txtPassword.Text;
        client.CommissionRate = txtCommission.Text;
        client.NECommission = txtNECommission.Text;
        client.ActualLimit = Convert.ToDecimal(txtLimit.Text);
        client.Limit = Convert.ToDecimal(txtLimit.Text);
        client.MatchTempLimit = Convert.ToDecimal(txtLimit.Text);
        client.MinLimit = Convert.ToDecimal(txtMinLimit.Text);
        client.MaxLimit = Convert.ToDecimal(txtMaxLimit.Text);
        client.IsActive = true;
        client.IsDeleted = false;
        client.IsLogin = true;
        client.Id = Convert.ToInt32(cookie["ClientId"]);
        client.DaeUpdated = System.DateTime.UtcNow;
        client.Update();
        layerSuccess.InnerHtml = "<strong>Success!</strong> Profile updated successfully.";
        layerSuccess.Attributes.Add("style", "display:block;");
    }
}